Option Explicit On 

Imports System.ComponentModel

Public Class TextBoxCustom
    Inherits System.Windows.Forms.TextBox

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Me.FocusedBackColor = Drawing.Color.FromArgb(255, 255, 255, 192)
        Me.FocusedForeColor = Drawing.Color.Black
    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    End Sub

#End Region

    Private _BackColor As Drawing.Color
    Private _ForeColor As Drawing.Color
    Private _FocusedBackColor As Drawing.Color
    Private _FocusedForeColor As Drawing.Color

    Private _tempBackColor As Drawing.Color
    Private _tempForeColor As Drawing.Color

    Private _Numeric As Boolean
    Private _Decimal As Integer


    <Category("Focus"), _
     Description("Define the BackColor of TextBox when GotFocus.")> _
    Public Property FocusedBackColor() As Drawing.Color
        Get
            Return _FocusedBackColor
        End Get
        Set(ByVal Value As Drawing.Color)
            _FocusedBackColor = Value
        End Set
    End Property

    <Category("Focus"), _
     Description("Define the ForeColor of TextBox when GotFocus.")> _
    Public Property FocusedForeColor() As Drawing.Color
        Get
            Return _FocusedForeColor
        End Get
        Set(ByVal Value As Drawing.Color)
            _FocusedForeColor = Value
        End Set
    End Property


    '   define numeric type
    <DefaultValue(False), _
     Category("Get/Set numeric validation")> _
     Public Property Numeric() As Boolean
        Get
            Return _Numeric
        End Get
        Set(ByVal Value As Boolean)
            _Numeric = Value
        End Set
    End Property


    '   check the Decimal value , default to 2 - .00
    <DefaultValue(0), _
     Category("Get/Set numeric format type, 0 - No Formating")> _
     Public Property [Decimal]() As Integer
        Get
            Return _Decimal
        End Get
        Set(ByVal Value As Integer)
            _Decimal = Value
        End Set
    End Property


    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)

        If (Not Me.ReadOnly) Then
            _tempBackColor = Me.BackColor
            _tempForeColor = Me.ForeColor
            Me.BackColor = _FocusedBackColor
            Me.ForeColor = _FocusedForeColor
        End If
        Me.SelectAll()

        MyBase.OnGotFocus(e)
    End Sub

    Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)

        If (Not Me.ReadOnly) Then
            'Me.BackColor = _BackColor
            Me.BackColor = _tempBackColor
            'Me.ForeColor = _ForeColor
            Me.ForeColor = _tempForeColor
        End If

        '   prequisition is checkNumeric and FormatCurrency
        If Me.Numeric And Me.Decimal > 0 Then
            Me.Text = FNumber(Me.Text)
        End If

        MyBase.OnLostFocus(e)
    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)


        If Me.ModifierKeys = Keys.Control AndAlso e.KeyCode = Keys.A Then
            Me.SelectAll()
        End If

        If Not Me.Multiline Then
            If e.KeyCode = Keys.Enter Then
                SendKeys.Send("{TAB}")
            End If
        End If

        MyBase.OnKeyDown(e)
    End Sub


    '   Internal checking numeric function
    Private Function FNumber(ByVal x As String) As String
        If IsNumeric(x) And Numeric Then
            Return FormatNumber(x, [Decimal])
        Else
            Return x
        End If
    End Function

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Me.Numeric Then
            If (Not Char.IsDigit(e.KeyChar) And _
                Not Char.IsControl(e.KeyChar) And _
                Not Char.IsPunctuation(e.KeyChar)) Then
                e.Handled = True
            End If
        End If

        'This is fired when a key is pressed in the text box
        'only do text change e when the key pressed is letter or digit
        MyBase.OnKeyPress(e)
    End Sub

End Class

